package com.whty.system.service.impl;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.whty.common.config.Global;
import com.whty.system.domain.DepotHeadItem;
import com.whty.system.domain.Garden;
import com.whty.system.domain.ProductDetail;
import com.whty.system.domain.PublishManagement;
import com.whty.system.domain.TagInfo;
import com.whty.system.mapper.DepotHeadItemMapper;
import com.whty.system.mapper.GardenMapper;
import com.whty.system.mapper.ProductDetailMapper;
import com.whty.system.mapper.PublishManagementMapper;
import com.whty.system.mapper.TagInfoMapper;
import com.whty.system.service.ITagService;
import com.whty.system.util.AESCoder;
import com.whty.system.util.Fn;
import com.whty.system.util.StringUtil;


/**
 * 客户 服务层实现
 * 
 * @author mario
 * @date 2018-10-23
 */
@Service
public class TagServiceImpl implements ITagService 
{
	@Autowired
	TagInfoMapper    tagTnfoMapper;
	@Autowired
    GardenMapper gardenMapper;
	@Autowired 
	PublishManagementMapper  publishManagementMapper;
	@Autowired 
	DepotHeadItemMapper  depotHeadItemMapper;
	@Autowired
	ProductDetailMapper  productDetailMapper;
	
    private static Logger logger = LoggerFactory.getLogger(TagServiceImpl.class);

	private String put(String sapdu,String apdu,String currentResp)
	{
		if("".equals(sapdu)||sapdu==null) return apdu+"|"+currentResp;
	     return sapdu+","+apdu+"|"+currentResp;	
	}
	
	/*
	 * 传入tagInfo,判断该标签信息 是否发行
	 * 返回 false ,true
	 */
	@Override
	public String tagIsUse(TagInfo tagInfo){
		/*
		 * 1.查询发行记录获取 uid
		 * 2.根据入库记录明细找出uid对应的分配 allocation_batch 分配批次
		 * 3.根据分配批次找出标签分配表 的分配标签 信息List<id>
		 * 4.查看传入的参数tagInfo 是否在 list<id>
		 */
		List<String> list = tagTnfoMapper.getTagInfoIdList();
		int id= tagInfo.getId();
		logger.info("传入的id是："+String.valueOf(id));
		if(list.contains(String.valueOf(id))){
			return "true";
			
		}else{
			return "false";
		}
	}

	@Override
	public String tagPublish(JSONObject msg) {
		JSONObject result=new JSONObject();
		String userId=msg.getString("userId");
		String uid=msg.getString("uid");
//		String stagkey00="44c438603458263c3a964026128a2858";
//		String stagkey01="5e76282c68d07ac26ec85ccc266878d4";
//		String stagkey02="1048086a647842244e087a6008407014";
		
		String Mkey00 =  "44c438603458263c3a964026128a2858";
		String 	Mkey01 =  "5e76282c68d07ac26ec85ccc266878d4";
        String Mkey02 =  "1048086a647842244e087a6008407014";
        DepotHeadItem dhi= depotHeadItemMapper.selectDepotHeadItemByUid(uid);
        String sn1 = dhi.getGardenNo()+"8000000000";
        String sdata2 = sn1 + Fn.xor(sn1, "FFFFFFFFFFFFFFFF");
        String Dkey00 = Fn.desecb(Mkey00, sdata2, 0);
        String Dkey01 = Fn.desecb(Mkey01, sdata2, 0);
        String Dkey02 =Fn.desecb(Mkey02, sdata2, 0);
        String sn2 = uid +"80";
        sdata2 = sn2 + Fn.xor(sn2, "FFFFFFFFFFFFFFFF");

        String stagkey00 =  Fn.desecb(Dkey00, sdata2, 0);
        String stagkey01 = Fn.desecb(Dkey01, sdata2, 0);
        String stagkey02 =  Fn.desecb(Dkey02, sdata2, 0);
		
        
		
		String soldkey00="00000000000000000000000000000000";
		String soldkey01="00000000000000000000000000000000";
		String soldkey02="00000000000000000000000000000000";
		
		String keyA = "66fa131649bb6f1174ad4eae5d240588";
		String keyB = "55797cfe285248db272516430de56f3c";
		
//		    String sSerialnum = "00000000000001";//14 length
//		//	;溯源码
//			String sBackCode = "16463511067399911342";// 20 length
//	//		;生产企业名称+产品名称+原料来源+加工工艺+生产日期+批号+产品质量等级+保质期+产品质量认证证书编号
//			String  compName="加工厂1";//生产企业名称
//			String s1=StringUtil.padding(Fn.asc(compName), 28, 3,2);
//			String productName="高级普洱";
//			String s2=StringUtil.padding(Fn.asc(productName), 28, 3,2);
//			String goodsFrom="茶园1";
//			String s3=StringUtil.padding(Fn.asc(goodsFrom), 28, 3,2);
//			String productMethod="1000";
//			String s4=StringUtil.padding(Fn.asc(productMethod), 8, 3,2);
////			String s5=new SimpleDateFormat("YYMMDD").format(new Date());
//			String s5="181010";
//			String s6="201810100003"+Fn.xor2("201810100003", "00");
//			String s7="01";
//			String s8="201010";
//			String s9=StringUtil.padding(Fn.asc("SA-")+"2018090101",20,3,2);
//			String sproductdata=s1+s2+s3+s4+s5+s6+s7+s8+s9;
//			String sdata1=sSerialnum+sBackCode+sproductdata+"800000000000000000";
//			String sIV1="00000000000000000000000000000000";
//			String sencdata1=AESCoder.cbcEnc(sdata1, keyA,sIV1);
//			String sproductchecksum=Fn.xor3(sencdata1, "0000");
//			String bName="";//经销商名称
//			String s10=StringUtil.padding(Fn.asc(bName),24,3,2);
//			String saleDate="000000";
//			String s11=saleDate;
//			sdata1=sSerialnum+sBackCode+s10+s11;
//			sIV1="00000000000000000000000000000000";
//			sencdata1=AESCoder.cbcEnc(sdata1, keyB,sIV1);
//			String ssalechecksum=Fn.xor3(sencdata1, "0000");
//			String stagdata=sSerialnum+sBackCode+sproductdata+sproductchecksum+s10+s11+ssalechecksum;
//		 
		     
		        TagInfo ti=tagTnfoMapper.selectTagInfoByUid(uid);
		        if(ti==null)
		        {
		        	result.put("return_code", "SUCCESS");
					result.put("return_msg", "OK");
					result.put("result_code", "FAIL");
					result.put("error_code", "TAGNOTEXIST");
					result.put("error_msg", "标签信息不存在");
					return result.toString();
		        }
		        String sSerialnum = dhi.getLabelSerialNumber();
//	            String sSerialnum = ti.getTagQuence();//14 length
			//	;溯源码
				String sBackCode = ti.getProductCode();// 20 length
		//		;生产企业名称+产品名称+原料来源+加工工艺+生产日期+批号+产品质量等级+保质期+产品质量认证证书编号
				String  compName=ti.getCompanyName();//生产企业名称
				if(compName!=null&&!compName.equals("")){
					Garden gardenTemp=new Garden();
					gardenTemp.setTeaGardenNo(compName);
				    List<Garden> listtemp=gardenMapper.selectGardenList(gardenTemp);
				    if(listtemp!=null&&listtemp.size()>0){
				    	compName=listtemp.get(0).getGardenName();
				    }
					
				}
				
				
				String s1=StringUtil.padding(Fn.asc(compName), 28, 3,2);
				String productName=ti.getProductName();
				String s2=StringUtil.padding(Fn.asc(productName), 28, 3,2);
				String goodsFrom=ti.getMaterialSource();
				String s3=StringUtil.padding(Fn.asc(goodsFrom), 28, 3,2);
				String productMethod=ti.getProcessTech();
				String s4=StringUtil.padding(Fn.asc(productMethod), 8, 3,2);
//				String s5=new SimpleDateFormat("YYMMDD").format(new Date());
				String s5=ti.getProductDate();
				String s6=ti.getBatchNo();//+Fn.xor2(ti.getBatchNo(), "00");
				String s7=ti.getProductRank();
				String s8=ti.getExpiredDate();
				String s9=StringUtil.padding(Fn.asc(ti.getProductCertNum().substring(0, 3))+ti.getProductCertNum().substring(3, 13),20,3,2);
				String sproductdata=s1+s2+s3+s4+s5+s6+s7+s8+s9;
				String sdata1=sSerialnum+sBackCode+sproductdata+"800000000000000000";
				String sIV1="00000000000000000000000000000000";
				String sencdata1=AESCoder.cbcEnc(sdata1, keyA,sIV1);
				String sproductchecksum=Fn.xor3(sencdata1, "0000");
				String bName=ti.getDealerName()==null ? "" : ti.getDealerName();//经销商名称
				String s10=StringUtil.padding(Fn.asc(bName),24,3,2);
				String saleDate=ti.getSaleTime();
				String s11=saleDate;
				sdata1=sSerialnum+sBackCode+s10+s11;
				sIV1="00000000000000000000000000000000";
				sencdata1=AESCoder.cbcEnc(sdata1, keyB,sIV1);
				String ssalechecksum=Fn.xor3(sencdata1, "0000");
				String stagdata=sSerialnum+sBackCode+sproductdata+sproductchecksum+s10+s11+ssalechecksum;
			

		
		String step=msg.getString("step");
		if(step.equals("ONE"))
		{
			String apdus0="";
			apdus0=put(apdus0,"reset","");
			apdus0=put(apdus0,"00A4040007D276000085010100","9000");
			apdus0=put(apdus0,"9071000002000000","91af");
			result.put("return_code", "SUCCESS");
			result.put("return_msg", "OK");
			result.put("result_code", "SUCCESS");
			result.put("apdus", apdus0);
			result.put("nextstep", "TWO");
			return result.toString();
		}else if(step.equals("TWO"))
		{
			String apdus1="";
			String enrandB=msg.getString("respapdu");
			enrandB=enrandB.substring(0, enrandB.length()-4);
			String key00=soldkey00;
			String randB=AESCoder.ecbDec(enrandB, key00);
			String randB2=Fn.right(randB, 15)+Fn.left(randB, 1);
			String randA=Fn.randomHexString(32);
			String sdata=randA+randB2;
			String encdata=AESCoder.cbcEnc(sdata, key00,"00000000000000000000000000000000");
			String apdu="90AF000020"+encdata+"00";
			apdus1=put(apdus1,apdu,"9100");
			result.put("return_code", "SUCCESS");
			result.put("return_msg", "OK");
			result.put("result_code", "SUCCESS");
			result.put("apdus", apdus1);
			result.put("randA", randA);
			result.put("randB", randB);
			result.put("nextstep", "THREE");
			return result.toString();
		}else if(step.equals("THREE"))
		{
			String apdus2="";
			String key00=soldkey00;
			String enrandA2=msg.getString("respapdu");
			String randA=msg.getString("randA");
			String randB=msg.getString("randB");
			enrandA2=enrandA2.substring(0, enrandA2.length()-4);
			String sPlaindata=AESCoder.cbcDec(enrandA2, key00,"00000000000000000000000000000000");
			String sTI=Fn.left(sPlaindata, 4);
			String randA2=Fn.strmid(sPlaindata, 8, 32);
			String sPDcap2=Fn.strmid(sPlaindata, 40, 12);
			String sPCDcap2=Fn.strmid(sPlaindata, 52, 12);
			String srandA2=Fn.right(randA,15)+Fn.left(randA, 1);
			if (!randA2.equalsIgnoreCase(srandA2))
			{
				result.put("return_code", "SUCCESS");
				result.put("return_msg", "OK");
				result.put("result_code", "FAIL");
				result.put("error_code", "KEYVALID");
				result.put("error_msg", "key00认证失败");
				return result.toString();
			}
			String stmpA=Fn.strmid(randA, 4, 12);
			String stmpB=Fn.strmid(randB, 0, 12);
			String sV1="A55A00010080"+Fn.left(randA,2)+Fn.xor(stmpA,stmpB)+Fn.right(randB,10) + Fn.right(randA,8);
			String sV2="5AA500010080" + Fn.left(randA,2) + Fn.xor(stmpA,stmpB) + Fn.right(randB,10) + Fn.right(randA,8);
			String sKSesAuthENC = Fn.calcSessionKey(sV1,key00);
			String sKSesAuthMAC = Fn.calcSessionKey(sV2,key00);
			//更新密钥KEY00
			String sIV = "A55A" + sTI +"00000000000000000000";
			String sencIV=AESCoder.ecbEnc(sIV, sKSesAuthENC);
			String skeyver = "00";
			String sPlainData= stagkey00 +  skeyver +  "800000000000000000000000000000";
			String sEncData=AESCoder.cbcEnc(sPlainData, sKSesAuthENC, sencIV);
			String sCmdCtr = "0000";
			String sKeyNo = "00";
			String sInputdata = "C4" + sCmdCtr + sTI + sKeyNo + sEncData;
			String scmacdata = Fn.calcSessionKey(sInputdata,sKSesAuthMAC);
			String smac = Fn.strmid(scmacdata,2,2)+Fn.strmid(scmacdata,6,2)+Fn.strmid(scmacdata,10,2)+Fn.strmid(scmacdata,14,2)+Fn.strmid(scmacdata,18,2)+Fn.strmid(scmacdata,22,2)+Fn.strmid(scmacdata,26,2)+Fn.strmid(scmacdata,30,2);
			String scmddata = sKeyNo + sEncData + smac;
			Integer clen =Fn.strlen(scmddata);
			String hexcDGIlen = clen.toHexString(clen);
		    hexcDGIlen= StringUtil.padding(hexcDGIlen, 2, 1, 1);
			String apdu="90C40000"+hexcDGIlen+scmddata+"00";
			apdus2=put(apdus2,apdu,"9100");
			// 5 Key00认证
		
			apdus2=put(apdus2,"9071000002000000","91af");
			result.put("return_code", "SUCCESS");
			result.put("return_msg", "OK");
			result.put("result_code", "SUCCESS");
			result.put("apdus", apdus2);
			result.put("nextstep", "FOUR");
			return result.toString();
		}else if(step.equals("FOUR"))
		{
			String apdus3="";
			String key00=stagkey00;
			String enrandB=msg.getString("respapdu");
			enrandB=enrandB.substring(0, enrandB.length()-4);
			String randB=AESCoder.ecbDec(enrandB, key00);
			String randB2=Fn.right(randB, 15)+Fn.left(randB, 1);
			String randA=Fn.randomHexString(32);
			String sdata=randA+randB2;
			String encdata=AESCoder.cbcEnc(sdata, key00,"00000000000000000000000000000000");
			String  apdu="90AF000020"+encdata+"00";
			apdus3=put(apdus3,apdu,"9100");
			result.put("return_code", "SUCCESS");
			result.put("return_msg", "OK");
			result.put("result_code", "SUCCESS");
			result.put("apdus", apdus3);
			result.put("randA", randA);
			result.put("randB", randB);
			result.put("nextstep", "FIVE");
			return result.toString();
			
		}else if(step.equals("FIVE"))
		{
			String apdus4="";
			 String key00=stagkey00;
			 String enrandA2=msg.getString("respapdu");
			 String randA=msg.getString("randA");
			 String randB=msg.getString("randB");
			 enrandA2=enrandA2.substring(0, enrandA2.length()-4);
			 String sPlaindata=AESCoder.cbcDec(enrandA2, key00,"00000000000000000000000000000000");
			 String sTI=Fn.left(sPlaindata, 4);
			 String randA2=Fn.strmid(sPlaindata, 8, 32);
			 String sPDcap2=Fn.strmid(sPlaindata, 40, 12);
			 String sPCDcap2=Fn.strmid(sPlaindata, 52, 12);
			 String srandA2=Fn.right(randA,15)+Fn.left(randA, 1);
			if (!randA2.equalsIgnoreCase(srandA2))
			{
				result.put("return_code", "SUCCESS");
				result.put("return_msg", "OK");
				result.put("result_code", "FAIL");
				result.put("error_code", "KEYVALID");
				result.put("error_msg", "key00认证失败");
				return result.toString();
			}
			String stmpA = Fn.strmid(randA,4,12);
			String  stmpB = Fn.strmid(randB,0,12);
			String sV1 = "A55A00010080" + Fn.left(randA,2) + Fn.xor(stmpA,stmpB) + Fn.right(randB,10) + Fn.right(randA,8);
			String sV2 = "5AA500010080" + Fn.left(randA,2) + Fn.xor(stmpA,stmpB) + Fn.right(randB,10) + Fn.right(randA,8); 
			String  sKSesAuthENC = Fn.calcSessionKey(sV1,key00);
			String  sKSesAuthMAC = Fn.calcSessionKey(sV2,key00);
			 //6 更新密钥KEY01
			 
			 String sCrc32=Fn.Crc32(stagkey01, "FFFFFFFF");
			 sCrc32=Fn.xor(sCrc32, "FFFFFFFF");
			 sCrc32=Fn.strrev(sCrc32);
			 String sxorkey = Fn.xor(stagkey01,soldkey01);
			 String sIV = "A55A" + sTI +"00000000000000000000";
			 
			 String sencIV=AESCoder.ecbEnc(sIV, sKSesAuthENC);
			 String skeyver = "00";
			 String sPlainData=sxorkey +  skeyver +  sCrc32 + "8000000000000000000000";
			 String sEncData=AESCoder.cbcEnc(sPlainData, sKSesAuthENC,sencIV);
			 String sCmdCtr = "0000";
			 String sKeyNo = "01";
			 String sInputdata = "C4" + sCmdCtr + sTI + sKeyNo + sEncData;
			 String scmacdata = Fn.calcSessionKey(sInputdata,sKSesAuthMAC);
			 String smac = Fn.strmid(scmacdata,2,2)+Fn.strmid(scmacdata,6,2)+Fn.strmid(scmacdata,10,2)+Fn.strmid(scmacdata,14,2)+Fn.strmid(scmacdata,18,2)+Fn.strmid(scmacdata,22,2)+Fn.strmid(scmacdata,26,2)+Fn.strmid(scmacdata,30,2);
			 String scmddata = sKeyNo + sEncData + smac;
			 Integer clen =Fn.strlen(scmddata);
			 String hexcDGIlen = clen.toHexString(clen);
			 hexcDGIlen= StringUtil.padding(hexcDGIlen, 2, 1, 1);
			 String apdu="90C40000"+hexcDGIlen +scmddata +"00";
			 apdus4=put(apdus4,apdu,"9100");
			 apdus4=put(apdus4,"9071000002000000","91af");
			 result.put("return_code", "SUCCESS");
		     result.put("return_msg", "OK");
			 result.put("result_code", "SUCCESS");
			 result.put("apdus", apdus4);
			 result.put("nextstep", "SIX");
			 return result.toString();
		}else if(step.equals("SIX"))
		{
			String apdus5="";
			String key00=stagkey00;
			String enrandB=msg.getString("respapdu");
			enrandB=enrandB.substring(0, enrandB.length()-4);
			String randB=AESCoder.ecbDec(enrandB, key00);
			String randB2=Fn.right(randB, 15)+Fn.left(randB, 1);
			String randA=Fn.randomHexString(32);
			String sdata=randA+randB2;
			String encdata=AESCoder.cbcEnc(sdata, key00,"00000000000000000000000000000000");
			String apdu="90AF000020"+encdata+"00";
			apdus5=put(apdus5,apdu,"9100");
			result.put("return_code", "SUCCESS");
			result.put("return_msg", "OK");
			result.put("result_code", "SUCCESS");
			result.put("apdus", apdus5);
			result.put("randA", randA);
			result.put("randB", randB);
			result.put("nextstep", "SEVEN");
			return result.toString();
		}else if(step.equals("SEVEN"))
		{
			 String apdus6="";
			 String key00=stagkey00;
			 String enrandA2=msg.getString("respapdu");
			 String randA=msg.getString("randA");
			 String randB=msg.getString("randB");
			 enrandA2=enrandA2.substring(0, enrandA2.length()-4);
			 String  sPlaindata=AESCoder.cbcDec(enrandA2, key00,"00000000000000000000000000000000");
			 String sTI=Fn.left(sPlaindata, 4);
			 String randA2=Fn.strmid(sPlaindata, 8, 32);
			 String sPDcap2=Fn.strmid(sPlaindata, 40, 12);
			 String sPCDcap2=Fn.strmid(sPlaindata, 52, 12);
			 String srandA2=Fn.right(randA,15)+Fn.left(randA, 1);
			if (!randA2.equalsIgnoreCase(srandA2))
			{
				System.out.println("stop");
			}
			//更新密钥需要计算过程密钥
			String stmpA = Fn.strmid(randA,4,12);
			String stmpB = Fn.strmid(randB,0,12);
			String sV1 = "A55A00010080" + Fn.left(randA,2) + Fn.xor(stmpA,stmpB) + Fn.right(randB,10) + Fn.right(randA,8);
			String sV2 = "5AA500010080" + Fn.left(randA,2) + Fn.xor(stmpA,stmpB) + Fn.right(randB,10) + Fn.right(randA,8); 
			String sKSesAuthENC = Fn.calcSessionKey(sV1,key00);
			String sKSesAuthMAC = Fn.calcSessionKey(sV2,key00);
//			 //5 更新密钥KEY02
//			 
			 String sCrc32=Fn.Crc32(stagkey02, "FFFFFFFF");
			 sCrc32=Fn.xor(sCrc32, "FFFFFFFF");
			 sCrc32=Fn.strrev(sCrc32);
			 String sxorkey = Fn.xor(stagkey02,soldkey02);
			 String sIV = "A55A" + sTI +"00000000000000000000";
			 String sencIV=AESCoder.ecbEnc(sIV, sKSesAuthENC);
			 String skeyver = "00";
			 String sPlainData=sxorkey +  skeyver +  sCrc32 + "8000000000000000000000";
			 String sEncData=AESCoder.cbcEnc(sPlainData, sKSesAuthENC,sencIV);
			 String sCmdCtr = "0000";
			 String sKeyNo = "02";
			 String sInputdata = "C4" + sCmdCtr + sTI + sKeyNo + sEncData;
			 String scmacdata = Fn.calcSessionKey(sInputdata,sKSesAuthMAC);
			 String smac = Fn.strmid(scmacdata,2,2)+Fn.strmid(scmacdata,6,2)+Fn.strmid(scmacdata,10,2)+Fn.strmid(scmacdata,14,2)+Fn.strmid(scmacdata,18,2)+Fn.strmid(scmacdata,22,2)+Fn.strmid(scmacdata,26,2)+Fn.strmid(scmacdata,30,2);
			 String scmddata = sKeyNo + sEncData + smac;
			 Integer clen =Fn.strlen(scmddata);
			 String hexcDGIlen = clen.toHexString(clen);
			  hexcDGIlen= StringUtil.padding(hexcDGIlen, 2, 1, 1);
			 String apdu="90C40000"+hexcDGIlen +scmddata +"00";
			 apdus6=put(apdus6,apdu,"9100");
			 apdus6=put(apdus6,"9071000002000000","91af");
			 result.put("return_code", "SUCCESS");
			 result.put("return_msg", "OK");
				result.put("result_code", "SUCCESS");
				result.put("apdus", apdus6);
				result.put("nextstep", "EIGHT");
				return result.toString();
		}else if(step.equals("EIGHT"))
		{
			String apdus7="";
			String key00=stagkey00;
			String enrandB=msg.getString("respapdu");
			enrandB=enrandB.substring(0, enrandB.length()-4);
			String randB=AESCoder.ecbDec(enrandB, key00);
			String  randB2=Fn.right(randB, 15)+Fn.left(randB, 1);
			String  randA=Fn.randomHexString(32);
			String  sdata=randA+randB2;
			String  encdata=AESCoder.cbcEnc(sdata, key00,"00000000000000000000000000000000");
			String apdu="90AF000020"+encdata+"00";
			apdus7=put(apdus7,apdu,"9100");
			result.put("return_code", "SUCCESS");
			result.put("return_msg", "OK");
			result.put("result_code", "SUCCESS");
			result.put("apdus", apdus7);
			result.put("randA", randA);
			result.put("randB", randB);
			result.put("nextstep", "NINE");
			return result.toString();
		}else if(step.equals("NINE"))
		{
			String apdus8="";
			String key00=stagkey00;
			String enrandA2=msg.getString("respapdu");
			 String randA=msg.getString("randA");
			 String randB=msg.getString("randB");
			 enrandA2=enrandA2.substring(0, enrandA2.length()-4);
			String  sPlaindata=AESCoder.cbcDec(enrandA2, key00,"00000000000000000000000000000000");
			String sTI=Fn.left(sPlaindata, 4);
			String randA2=Fn.strmid(sPlaindata, 8, 32);
			String sPDcap2=Fn.strmid(sPlaindata, 40, 12);
			String sPCDcap2=Fn.strmid(sPlaindata, 52, 12);
			String srandA2=Fn.right(randA,15)+Fn.left(randA, 1);
			if (!randA2.equalsIgnoreCase(srandA2))
			{
				result.put("return_code", "SUCCESS");
				result.put("return_msg", "OK");
				result.put("result_code", "FAIL");
				result.put("error_code", "KEYVALID");
				result.put("error_msg", "key00认证失败");
				return result.toString();
			}
			String apdu="908D000071020000006A0000"+ stagdata+"00";	
			apdus8=put(apdus8,apdu,"9100");
			apdus8=put(apdus8,"9071000002000000","91af");
			result.put("return_code", "SUCCESS");
			result.put("return_msg", "OK");
			result.put("result_code", "SUCCESS");
			result.put("apdus", apdus8);
			result.put("nextstep", "TEN");
			return result.toString();
		}else if(step.equals("TEN"))
		{
			String apdus9="";
			String key00=stagkey00;
			String enrandB=msg.getString("respapdu");
			enrandB=enrandB.substring(0, enrandB.length()-4);
			String randB=AESCoder.ecbDec(enrandB, key00);
			String randB2=Fn.right(randB, 15)+Fn.left(randB, 1);
			String  randA=Fn.randomHexString(32);
			String  sdata=randA+randB2;
			String encdata=AESCoder.cbcEnc(sdata, key00,"00000000000000000000000000000000");
			String  apdu="90AF000020"+encdata+"00";
			apdus9=put(apdus9,apdu,"9100");
			result.put("return_code", "SUCCESS");
			result.put("return_msg", "OK");
			result.put("result_code", "SUCCESS");
			result.put("apdus", apdus9);
			result.put("randA", randA);
			result.put("randB", randB);
			result.put("nextstep", "ELEVEN");
			return result.toString();
		}else if(step.equals("ELEVEN"))
		{
			String apdus10="";
			String key00=stagkey00;
			String enrandA2=msg.getString("respapdu");
			String randA=msg.getString("randA");
			String randB=msg.getString("randB");
			enrandA2=enrandA2.substring(0, enrandA2.length()-4);
			String sPlaindata=AESCoder.cbcDec(enrandA2, key00,"00000000000000000000000000000000");
			String sTI=Fn.left(sPlaindata, 4);
			String randA2=Fn.strmid(sPlaindata, 8, 32);
			String sPDcap2=Fn.strmid(sPlaindata, 40, 12);
			String sPCDcap2=Fn.strmid(sPlaindata, 52, 12);
			String srandA2=Fn.right(randA,15)+Fn.left(randA, 1);
			if (!randA2.equalsIgnoreCase(srandA2))
			{
				result.put("return_code", "SUCCESS");
				result.put("return_msg", "OK");
				result.put("result_code", "FAIL");
				result.put("error_code", "KEYVALID");
				result.put("error_msg", "key00认证失败");
				return result.toString();
			}
			apdus10=put(apdus10,"00A4040007D276000085010100","9000");
			apdus10=put(apdus10,"00A4000C02E104","9000");
			apdus10=put(apdus10,"00B0000000","9000");
			result.put("return_code", "SUCCESS");
			result.put("return_msg", "OK");
			result.put("result_code", "SUCCESS");
			result.put("apdus", apdus10);
			result.put("nextstep", "TWELVE");
			return result.toString();
		}else if(step.equals("TWELVE"))
		{
			
			String res=msg.getString("respapdu");
			res=res.substring(0,res.length()-4);
			String rdata = Fn.left(res,106);
			String sCNT = Fn.strmid(res,212,12);
			String scounter = Fn.Hex(res,106,6);
			String rcmac = Fn.Hex(res,112,16);
//			
			String key00=stagkey01;
			String sdata = stagdata+sCNT;
           String  SV2 = "3CC300010080" + Fn.strrev(scounter) + "00000000000000";
           String sKSesMAC = Fn.calcSessionKey(SV2,key00);
           String smacdata = Fn.calcSessionKey(sdata, sKSesMAC);
		   String smac = Fn.strmid(smacdata,2,2)+Fn.strmid(smacdata,6,2)+Fn.strmid(smacdata,10,2)+Fn.strmid(smacdata,14,2)+Fn.strmid(smacdata,18,2)+Fn.strmid(smacdata,22,2)+Fn.strmid(smacdata,26,2)+Fn.strmid(smacdata,30,2);
			 if(!rcmac.equalsIgnoreCase(smac))
			 {
				    result.put("return_code", "SUCCESS");
					result.put("return_msg", "OK");
					result.put("result_code", "FAIL");
					result.put("error_code", "MACVALID");
					result.put("error_msg", "MAC认证失败");
					return result.toString();
			 }
			
			sdata = Fn.left(rdata, 87) +"800000000000000000";
		   String sIV = "00000000000000000000000000000000";
           String  sencdata =AESCoder.cbcEnc(sdata, keyA,sIV);
			String schksum1 = Fn.xor3(sencdata,"0000") ;
			if(!schksum1.equalsIgnoreCase(Fn.strmid(rdata,174,4)))
			{
				result.put("return_code", "SUCCESS");
				result.put("return_msg", "OK");
				result.put("result_code", "FAIL");
				result.put("error_code", "PRODUCTINFOVALID");
				result.put("error_msg", "生产信息校验失败");
				return result.toString();
			}
			 sdata = Fn.left(rdata, 17) + Fn.strmid(rdata,178,30);
			 sIV = "00000000000000000000000000000000";
			
			sencdata = AESCoder.cbcEnc(sdata, keyB,sIV);
			String schksum2 = Fn.xor3(sencdata,"0000");
			if(!schksum2.equalsIgnoreCase( Fn.strmid(rdata,208,4)))
			{
				result.put("return_code", "SUCCESS");
				result.put("return_msg", "OK");
				result.put("result_code", "FAIL");
				result.put("error_code", "SALEINFOVALID");
				result.put("error_msg", "销售信息校验失败");
				return result.toString();
			}else
			{
				PublishManagement pm=new PublishManagement();
				pm.setOperator(userId);
				pm.setOperationType("00");
				pm.setOperationResult("00");
				pm.setUid(uid);
				pm.setProductCode(ti.getProductCode());
				pm.setGardenNo(ti.getCompanyName());
				pm.setOperationDate(new Date());
				publishManagementMapper.insertPublishManagement(pm);
				dhi.setReleaseStatus("02");
				depotHeadItemMapper.updateDepotHeadItem(dhi);
			    result.put("return_code", "SUCCESS");
			    result.put("return_msg", "标签发布成功");
				result.put("result_code", "SUCCESS");
			    result.put("nextstep", "END");
				return result.toString();
			}
			
			
		}
			
			
			
			
			
			
		return result.toString();
	}

	class TagPublishResult
	{
		private String return_code;
		private String return_msg;
		private String result_code;
		private String apdus;
		public TagPublishResult(String return_code, String return_msg, String result_code, String apdus,
				String nextstep) {
			super();
			this.return_code = return_code;
			this.return_msg = return_msg;
			this.result_code = result_code;
			this.apdus = apdus;
			this.nextstep = nextstep;
		}
		public String getReturn_code() {
			return return_code;
		}
		public void setReturn_code(String return_code) {
			this.return_code = return_code;
		}
		public String getReturn_msg() {
			return return_msg;
		}
		public void setReturn_msg(String return_msg) {
			this.return_msg = return_msg;
		}
		public String getResult_code() {
			return result_code;
		}
		public void setResult_code(String result_code) {
			this.result_code = result_code;
		}
		public String getApdus() {
			return apdus;
		}
		public void setApdus(String apdus) {
			this.apdus = apdus;
		}
		public String getNextstep() {
			return nextstep;
		}
		public void setNextstep(String nextstep) {
			this.nextstep = nextstep;
		}
		private String nextstep;
	}
	
	


	public static void main(String args[])
	{
//		        String keyA = "66fa131649bb6f1174ad4eae5d240588";
//			//	;平台销售信息校验密钥
//				String keyB = "55797cfe285248db272516430de56f3c";
//
//			//	;标签序列号
//				String sSerialnum = "00000000000001";//14 length
//			//	;溯源码
//				String sBackCode = "16463511067399911342";// 20 length
//		//		;生产企业名称+产品名称+原料来源+加工工艺+生产日期+批号+产品质量等级+保质期+产品质量认证证书编号
//				String  compName="加工厂1";//生产企业名称
//				String s1=StringUtil.padding(Fn.asc(compName), 28, 3,2);
//				String productName="高级普洱";
//				String s2=StringUtil.padding(Fn.asc(productName), 28, 3,2);
//				String goodsFrom="茶园1";
//				String s3=StringUtil.padding(Fn.asc(goodsFrom), 28, 3,2);
//				String productMethod="1000";
//				String s4=StringUtil.padding(Fn.asc(productMethod), 8, 3,2);
//				
////				String s5=new SimpleDateFormat("YYMMDD").format(new Date());
//				String s5="181010";
//				String s6="201810100003"+Fn.xor2("201810100003", "00");
//				String s7="01";
//				String s8="201010";
//				String s9=StringUtil.padding(Fn.asc("SA-")+"2018090101",20,3,2);
//				String sproductdata=s1+s2+s3+s4+s5+s6+s7+s8+s9;
//				String sdata=sSerialnum+sBackCode+sproductdata+"800000000000000000";
//				String sIV="00000000000000000000000000000000";
//				String sencdata=AESCoder.cbcEnc(sdata, keyA,sIV);
//				String sproductchecksum=Fn.xor3(sencdata, "0000");
//				String bName="";//经销商名称
//				String s10=StringUtil.padding(Fn.asc(bName),24,3,2);
//				String saleDate="000000";
//				String s11=saleDate;
//				sdata=sSerialnum+sBackCode+s10+s11;
//				sIV="00000000000000000000000000000000";
//				sencdata=AESCoder.cbcEnc(sdata, keyB,sIV);
//				String ssalechecksum=Fn.xor3(sencdata, "0000");
//				String stagdata=sSerialnum+sBackCode+sproductdata+sproductchecksum+s10+s11+ssalechecksum;
//				System.out.println(stagdata);
//		String res="0000000000000116463511067399911342bcd3b9a4b3a73120202020202020b8dfbcb6c6d5b6fd202020202020b2e8d4b031202020202020202020313030301810102018101000033b0120101053412d201809010120209caa2020202020202020202020200000007d1230303030303332323637323037314636354446374237";
//		String rdata = Fn.left(res,106);
//		String sCNT = Fn.strmid(res,212,12);
//		String scounter = Fn.Hex(res,106,6);
//		String rcmac = Fn.Hex(res,112,16);
////		
//		String key00="5e76282c68d07ac26ec85ccc266878d4";
//		String sdata = stagdata+sCNT;
//       String  SV2 = "3CC300010080" + Fn.strrev(scounter) + "00000000000000";
//       String sKSesMAC = Fn.calcSessionKey(SV2,key00);
//       String smacdata = Fn.calcSessionKey(sdata, sKSesMAC);
//	   String smac = Fn.strmid(smacdata,2,2)+Fn.strmid(smacdata,6,2)+Fn.strmid(smacdata,10,2)+Fn.strmid(smacdata,14,2)+Fn.strmid(smacdata,18,2)+Fn.strmid(smacdata,22,2)+Fn.strmid(smacdata,26,2)+Fn.strmid(smacdata,30,2);
//		 if(!rcmac.equalsIgnoreCase(smac))
//		 {
//			   
//		 }
		
//	String sdata = Fn.left(rdata, 87) +"800000000000000000";
//	   String sIV = "00000000000000000000000000000000";
//       String  sencdata =AESCoder.cbcEnc(sdata, keyA,sIV);
//		String schksum1 = Fn.xor3(sencdata,"0000") ;
//		if(!schksum1.equalsIgnoreCase(Fn.strmid(rdata,174,4)))
//		{
//			System.out.println("校验失败");
//		}
//		 sdata = Fn.left(rdata, 17) + Fn.strmid(rdata,178,30);
//		 sIV = "00000000000000000000000000000000";
//		
//		sencdata = AESCoder.cbcEnc(sdata, keyB,sIV);
//		String schksum2 = Fn.xor3(sencdata,"0000");
//		if(!schksum2.equalsIgnoreCase( Fn.strmid(rdata,208,4)))
//		{
//			System.out.println("校验失败");
//		}else
//		{
//			System.out.println("校验成功");
//		}
		// 33445566778899
//		System.out.println(Fn.xor3("112233445566778899", "0000"));//1810100003
		
//		System.out.println(Fn.xor("38", "03"));
				
//				System.out.println("test:"+Fn.asc("181010"));
		String Mkey00 =  "44c438603458263c3a964026128a2858";
		String 	Mkey01 =  "5e76282c68d07ac26ec85ccc266878d4";
        String Mkey02 =  "1048086a647842244e087a6008407014";
		
        String sn1 = "5003018000000000";
        String sdata = sn1 + Fn.xor(sn1, "FFFFFFFFFFFFFFFF");
        String Dkey00 = Fn.desecb(Mkey00, sdata, 0);
        String Dkey01 = Fn.desecb(Mkey01, sdata, 0);
        String Dkey02 =Fn.desecb(Mkey02, sdata, 0);
        String uid="043805925a5780";
        String sn2 = uid +"80";
        sdata = sn2 + Fn.xor(sn2, "FFFFFFFFFFFFFFFF");

        String stagkey00 =  Fn.desecb(Dkey00, sdata, 0);
        String stagkey01 = Fn.desecb(Dkey01, sdata, 0);
        String stagkey02 =  Fn.desecb(Dkey02, sdata, 0);
        System.out.println(stagkey00);
        System.out.println(stagkey01);
        System.out.println(stagkey02);
	}
    
	
	/*
	 * (non-Javadoc)
	 * @see com.whty.system.service.ITagService#tagAuth(com.alibaba.fastjson.JSONObject)
	 */
	public void printmiyao(String uid){
		String Mkey00 =  "44c438603458263c3a964026128a2858";
		String 	Mkey01 =  "5e76282c68d07ac26ec85ccc266878d4";
        String Mkey02 =  "1048086a647842244e087a6008407014";
        DepotHeadItem dhi= depotHeadItemMapper.selectDepotHeadItemByUid(uid);
        String sn1 = dhi.getGardenNo()+"8000000000";
        String sdata2 = sn1 + Fn.xor(sn1, "FFFFFFFFFFFFFFFF");
        String Dkey00 = Fn.desecb(Mkey00, sdata2, 0);
        String Dkey01 = Fn.desecb(Mkey01, sdata2, 0);
        String Dkey02 =Fn.desecb(Mkey02, sdata2, 0);
        String sn2 = uid +"80";
        sdata2 = sn2 + Fn.xor(sn2, "FFFFFFFFFFFFFFFF");

        String stagkey00 =  Fn.desecb(Dkey00, sdata2, 0);
        String stagkey01 = Fn.desecb(Dkey01, sdata2, 0);
        String stagkey02 =  Fn.desecb(Dkey02, sdata2, 0);
		logger.info("stagkey00:"+stagkey00);
		logger.info("stagkey01:"+stagkey01);
		logger.info("stagkey02:"+stagkey02);
		
	}
	@Override
	public String tagAuth(JSONObject msg) {
		JSONObject result=new JSONObject();
		String uid=msg.getString("uid");
      TagInfo ti=tagTnfoMapper.selectTagInfoByUid(uid);
		if(ti==null)
		{
			result.put("return_code", "SUCCESS");
			result.put("return_msg", "OK");
			result.put("result_code", "FAIL");
			result.put("error_code", "TAGNOTEXIST");
			result.put("error_msg", "标签信息不存在");
			return result.toString();
		}
		
		printmiyao(uid);
		
		
		String 	Mkey01 =  "5e76282c68d07ac26ec85ccc266878d4";
        DepotHeadItem dhi= depotHeadItemMapper.selectDepotHeadItemByUid(uid);
        String sn1 = dhi.getGardenNo()+"8000000000";
        String sdata2 = sn1 + Fn.xor(sn1, "FFFFFFFFFFFFFFFF");
        String Dkey01 = Fn.desecb(Mkey01, sdata2, 0);
        String sn2 = uid +"80";
        sdata2 = sn2 + Fn.xor(sn2, "FFFFFFFFFFFFFFFF");
        String stagkey01 = Fn.desecb(Dkey01, sdata2, 0);
        
        
		String res=msg.getString("respapdu");
		res=res.substring(0,res.length()-4);
		String rdata = Fn.left(res,112);
		String scounter = Fn.Hex(res,106,6);
		String rcmac = Fn.Hex(res,112,16);
//		
//		String stagkey01 =  "5e76282c68d07ac26ec85ccc266878d4";
		String key00=stagkey01;
		String keyA = "66fa131649bb6f1174ad4eae5d240588";
		String keyB = "55797cfe285248db272516430de56f3c";
       String  SV2 = "3CC300010080" + Fn.strrev(scounter) + "00000000000000";
       String sKSesMAC = Fn.calcSessionKey(SV2,key00);
       String smacdata = Fn.calcSessionKey(rdata, sKSesMAC);
	   String smac = Fn.strmid(smacdata,2,2)+Fn.strmid(smacdata,6,2)+Fn.strmid(smacdata,10,2)+Fn.strmid(smacdata,14,2)+Fn.strmid(smacdata,18,2)+Fn.strmid(smacdata,22,2)+Fn.strmid(smacdata,26,2)+Fn.strmid(smacdata,30,2);
		 if(!rcmac.equalsIgnoreCase(smac))
		 {
			    result.put("return_code", "SUCCESS");
				result.put("return_msg", "OK");
				result.put("result_code", "FAIL");
				result.put("error_code", "MACVALID");
				result.put("error_msg", "MAC认证失败");
				return result.toString();
		 }
		
		String sdata = Fn.left(rdata, 87) +"800000000000000000";
	   String sIV = "00000000000000000000000000000000";
       String  sencdata =AESCoder.cbcEnc(sdata, keyA,sIV);
		String schksum1 = Fn.xor3(sencdata,"0000") ;
		if(!schksum1.equalsIgnoreCase(Fn.strmid(rdata,174,4)))
		{
			result.put("return_code", "SUCCESS");
			result.put("return_msg", "OK");
			result.put("result_code", "FAIL");
			result.put("error_code", "PRODUCTINFOVALID");
			result.put("error_msg", "生产信息校验失败");
			return result.toString();
		}
		 sdata = Fn.left(rdata, 17) + Fn.strmid(rdata,178,30);
		 sIV = "00000000000000000000000000000000";
		
		sencdata = AESCoder.cbcEnc(sdata, keyB,sIV);
		String schksum2 = Fn.xor3(sencdata,"0000");
		if(!schksum2.equalsIgnoreCase( Fn.strmid(rdata,208,4)))
		{
			result.put("return_code", "SUCCESS");
			result.put("return_msg", "OK");
			result.put("result_code", "FAIL");
			result.put("error_code", "SALEINFOVALID");
			result.put("error_msg", "销售信息校验失败");
			return result.toString();
		}else
		{
			PublishManagement pm=new PublishManagement();
			pm.setOperationType("01");
			pm.setOperationResult("00");
			pm.setUid(uid);
			pm.setProductCode(ti.getProductCode());
			pm.setGardenNo(ti.getCompanyName());
			pm.setOperationDate(new Date());
			publishManagementMapper.insertPublishManagement(pm);
			    result.put("return_code", "SUCCESS");
				result.put("return_msg", "标签认证成功");
				result.put("result_code", "SUCCESS");
				result.put("nextstep", "END");
				return result.toString();
		}
		
	}

	@Override
	public String tagUpdate(JSONObject msg) {
		        JSONObject result=new JSONObject();
		        String uid=msg.getString("uid");
		        String step=msg.getString("step");
//		        String stagkey00 =  "44c438603458263c3a964026128a2858";
//				String stagkey01 =  "5e76282c68d07ac26ec85ccc266878d4";
//				String stagkey02 =  "1048086a647842244e087a6008407014";
		        
		        String Mkey00 =  "44c438603458263c3a964026128a2858";
				String 	Mkey01 =  "5e76282c68d07ac26ec85ccc266878d4";
		        String Mkey02 =  "1048086a647842244e087a6008407014";
		        DepotHeadItem dhi= depotHeadItemMapper.selectDepotHeadItemByUid(uid);
		        String sn1 = dhi.getGardenNo()+"8000000000";
		        String sdata2 = sn1 + Fn.xor(sn1, "FFFFFFFFFFFFFFFF");
		        String Dkey00 = Fn.desecb(Mkey00, sdata2, 0);
		        String Dkey01 = Fn.desecb(Mkey01, sdata2, 0);
		        String Dkey02 =Fn.desecb(Mkey02, sdata2, 0);
		        String sn2 = uid +"80";
		        sdata2 = sn2 + Fn.xor(sn2, "FFFFFFFFFFFFFFFF");

		        String stagkey00 =  Fn.desecb(Dkey00, sdata2, 0);
		        String stagkey01 = Fn.desecb(Dkey01, sdata2, 0);
		        String stagkey02 =  Fn.desecb(Dkey02, sdata2, 0);
				

//				;平台生产信息校验密钥
				String keyA = "66fa131649bb6f1174ad4eae5d240588";
//				;平台销售信息校验密钥
				String keyB = "55797cfe285248db272516430de56f3c";

//				;标签更新数据准备

//				;标签序列号
//				String sSerialnum = "00000000000001";
////				;溯源码
//				String sBackCode = "16463511067399911342";
//				String saleMan="销售部1";
//				String saleDate="181029";
				
			    TagInfo ti=tagTnfoMapper.selectTagInfoByUid(uid);
			    if(ti==null)
		        {
		        	result.put("return_code", "SUCCESS");
					result.put("return_msg", "OK");
					result.put("result_code", "FAIL");
					result.put("error_code", "TAGNOTEXIST");
					result.put("error_msg", "标签信息不存在");
					return result.toString();
		        }
//			    String sSerialnum=ti.getTagQuence();
			    String sSerialnum=dhi.getLabelSerialNumber();
			    String sBackCode=ti.getProductCode();
			    String saleMan=ti.getDealerName();
			    String saleDate=ti.getSaleTime();
//				;经销商名称+销售时间
				String s10=StringUtil.padding(Fn.asc(saleMan), 24, 3, 2);
				String s11=saleDate;

				String sdata1 = sSerialnum + sBackCode + s10 + s11;
				String sIV = "00000000000000000000000000000000";
				String sencdata =AESCoder.cbcEnc(sdata1, keyB,sIV);
//				;销售信息校验码
				String ssalechecksum = Fn.xor3(sencdata,"0000"); 

				String stagdata = s10 + s11 + ssalechecksum;
				if(step.equals("ONE"))
				{
					String apdus0="";
					apdus0=put(apdus0,"reset","");
					apdus0=put(apdus0,"00A4040007D276000085010100","9000");
					apdus0=put(apdus0,"9071000002000000","91af");
					result.put("return_code", "SUCCESS");
					result.put("return_msg", "OK");
					result.put("result_code", "SUCCESS");
					result.put("apdus", apdus0);
					result.put("nextstep", "TWO");
					return result.toString();
				}else if(step.equals("TWO"))
				{
					String apdus1="";
					String enrandB=msg.getString("respapdu");
					enrandB=enrandB.substring(0, enrandB.length()-4);
					String key00=stagkey00;
					String randB=AESCoder.ecbDec(enrandB, key00);
					String randB2=Fn.right(randB, 15)+Fn.left(randB, 1);
					String randA=Fn.randomHexString(32);
					String sdata=randA+randB2;
					String encdata=AESCoder.cbcEnc(sdata, key00,"00000000000000000000000000000000");
					String apdu="90AF000020"+encdata+"00";
					apdus1=put(apdus1,apdu,"9100");
					result.put("return_code", "SUCCESS");
					result.put("return_msg", "OK");
					result.put("result_code", "SUCCESS");
					result.put("apdus", apdus1);
					result.put("randA", randA);
					result.put("randB", randB);
					result.put("nextstep", "THREE");
					return result.toString();
				}else if(step.equals("THREE"))
				{
					String apdus2="";
					String key00=stagkey00;
					String enrandA2=msg.getString("respapdu");
					String randA=msg.getString("randA");
					String randB=msg.getString("randB");
					enrandA2=enrandA2.substring(0, enrandA2.length()-4);
					String sPlaindata=AESCoder.cbcDec(enrandA2, key00,"00000000000000000000000000000000");
					String sTI=Fn.left(sPlaindata, 4);
					String randA2=Fn.strmid(sPlaindata, 8, 32);
					String sPDcap2=Fn.strmid(sPlaindata, 40, 12);
					String sPCDcap2=Fn.strmid(sPlaindata, 52, 12);
					String srandA2=Fn.right(randA,15)+Fn.left(randA, 1);
					if (!randA2.equalsIgnoreCase(srandA2))
					{
						result.put("return_code", "SUCCESS");
						result.put("return_msg", "OK");
						result.put("result_code", "FAIL");
						result.put("error_code", "KEYVALID");
						result.put("error_msg", "key00认证失败");
						return result.toString();
					}
					String apdu="908D00001802590000110000"+ stagdata +"00";
					apdus2=put(apdus2,apdu,"9100");
					apdus2=put(apdus2,"9071000002000000","91af");
					result.put("return_code", "SUCCESS");
					result.put("return_msg", "OK");
					result.put("result_code", "SUCCESS");
					result.put("apdus", apdus2);
					result.put("nextstep", "FOUR");
					return result.toString();
				}else if(step.equals("FOUR"))
				{
					String apdus3="";
					String key00=stagkey00;
					String enrandB=msg.getString("respapdu");
					enrandB=enrandB.substring(0, enrandB.length()-4);
					String randB=AESCoder.ecbDec(enrandB, key00);
					String randB2=Fn.right(randB, 15)+Fn.left(randB, 1);
					String  randA=Fn.randomHexString(32);
					String  sdata=randA+randB2;
					String encdata=AESCoder.cbcEnc(sdata, key00,"00000000000000000000000000000000");
					String  apdu="90AF000020"+encdata+"00";
					apdus3=put(apdus3,apdu,"9100");
					result.put("return_code", "SUCCESS");
					result.put("return_msg", "OK");
					result.put("result_code", "SUCCESS");
					result.put("apdus", apdus3);
					result.put("randA", randA);
					result.put("randB", randB);
					result.put("nextstep", "FIVE");
					return result.toString();
				}else if(step.equals("FIVE"))
				{
					String apdus4="";
					String key00=stagkey00;
					String enrandA2=msg.getString("respapdu");
					String randA=msg.getString("randA");
					String randB=msg.getString("randB");
					enrandA2=enrandA2.substring(0, enrandA2.length()-4);
					String sPlaindata=AESCoder.cbcDec(enrandA2, key00,"00000000000000000000000000000000");
					String sTI=Fn.left(sPlaindata, 4);
					String randA2=Fn.strmid(sPlaindata, 8, 32);
					String sPDcap2=Fn.strmid(sPlaindata, 40, 12);
					String sPCDcap2=Fn.strmid(sPlaindata, 52, 12);
					String srandA2=Fn.right(randA,15)+Fn.left(randA, 1);
					if (!randA2.equalsIgnoreCase(srandA2))
					{
						result.put("return_code", "SUCCESS");
						result.put("return_msg", "OK");
						result.put("result_code", "FAIL");
						result.put("error_code", "KEYVALID");
						result.put("error_msg", "key00认证失败");
						return result.toString();
					}
					apdus4=put(apdus4,"00A4040007D276000085010100","9000");
					apdus4=put(apdus4,"00A4000C02E104","9000");
					apdus4=put(apdus4,"00B0000000","9000");
					result.put("return_code", "SUCCESS");
					result.put("return_msg", "OK");
					result.put("result_code", "SUCCESS");
					result.put("apdus", apdus4);
					result.put("nextstep", "SIX");
					return result.toString();
				}else if(step.equals("SIX"))
				{
					String resp=msg.getString("respapdu");
					resp=resp.substring(0,resp.length()-4);
					String rdata = Fn.left(resp,106);
					 sdata1 = Fn.left(resp,89);
					String sCNT = Fn.strmid(resp,212,12);
					String scounter =Fn.Hex(resp,106,6);
					String rcmac = Fn.Hex(resp,112,16);
					
//					
					String key00=stagkey01;
					String sdata =sdata1 + stagdata + sCNT;
		           String  SV2 = "3CC300010080" + Fn.strrev(scounter) + "00000000000000";
		           String sKSesMAC = Fn.calcSessionKey(SV2,key00);
		           String smacdata = Fn.calcSessionKey(sdata, sKSesMAC);
				   String smac = Fn.strmid(smacdata,2,2)+Fn.strmid(smacdata,6,2)+Fn.strmid(smacdata,10,2)+Fn.strmid(smacdata,14,2)+Fn.strmid(smacdata,18,2)+Fn.strmid(smacdata,22,2)+Fn.strmid(smacdata,26,2)+Fn.strmid(smacdata,30,2);
					 if(!rcmac.equalsIgnoreCase(smac))
					 {
						    result.put("return_code", "SUCCESS");
							result.put("return_msg", "OK");
							result.put("result_code", "FAIL");
							result.put("error_code", "MACVALID");
							result.put("error_msg", "MAC认证失败");
							return result.toString();
					 }
					
					sdata = Fn.left(rdata, 87) +"800000000000000000";
				    sIV = "00000000000000000000000000000000";
		             sencdata =AESCoder.cbcEnc(sdata, keyA,sIV);
					String schksum1 = Fn.xor3(sencdata,"0000") ;
					if(!schksum1.equalsIgnoreCase(Fn.strmid(rdata,174,4)))
					{
						result.put("return_code", "SUCCESS");
						result.put("return_msg", "OK");
						result.put("result_code", "FAIL");
						result.put("error_code", "PRODUCTINFOVALID");
						result.put("error_msg", "生产信息校验失败");
						return result.toString();
					}
					 sdata = Fn.left(rdata, 17) + Fn.strmid(rdata,178,30);
					 sIV = "00000000000000000000000000000000";
					
					sencdata = AESCoder.cbcEnc(sdata, keyB,sIV);
					String schksum2 = Fn.xor3(sencdata,"0000");
					if(!schksum2.equalsIgnoreCase( Fn.strmid(rdata,208,4)))
					{
						result.put("return_code", "SUCCESS");
						result.put("return_msg", "OK");
						result.put("result_code", "FAIL");
						result.put("error_code", "SALEINFOVALID");
						result.put("error_msg", "销售信息校验失败");
						return result.toString();
					}else
					{
						    result.put("return_code", "SUCCESS");
							result.put("return_msg", "标签更新成功");
							result.put("result_code", "SUCCESS");
							result.put("nextstep", "END");
							return result.toString();
					}
				}
					

		return result.toString();
	}

	@Override
	public String publishRecord(JSONObject msg) {
		 JSONObject result=new JSONObject();
		String userId=msg.getString("userId");
		List<PublishManagement> pms=publishManagementMapper.selectPublishManagementByOperator(userId);
		result.put("return_code", "SUCCESS");
	    result.put("return_msg", "标签发行记录获取成功");
	    result.put("result_code", "SUCCESS");
	    result.put("publishrecord", pms);
		return result.toString();
	}

	@Override
	public String authRecord(JSONObject msg) {
		 JSONObject result=new JSONObject();
			String uid=msg.getString("uid");
			List<PublishManagement> pms=publishManagementMapper.selectAuthManagementByOperator(uid);
			result.put("return_code", "SUCCESS");
		    result.put("return_msg", "认证记录获取成功");
		    result.put("result_code", "SUCCESS");
		    result.put("authrecord", pms);
			return result.toString();
	}

	/*
	 * uid 序列号
	 * 第一步：uid 是否存在，不存在则为非法标签；
	 * 第二步：uid +序列号 是否与数据库对应一致；
	 * 第三步：标签的uid 是否已分配，没分配，该标签未分配
	 */
	@Override
	public String tagStatusQuery(JSONObject msg) {
		JSONObject result=new JSONObject();
	      String uid=msg.getString("uid");
	      String serialNo=msg.getString("serialNo");
	      DepotHeadItem  dhi= depotHeadItemMapper.selectDepotHeadItemByUid(uid);
	      if(dhi==null)
	      {
	    	  result.put("return_code", "SUCCESS");
			  result.put("return_msg", "标签uid不存在，非法标签");
			  result.put("result_code", "FAIL");
			  result.put("error_code", "GETSTATUSFAIL");
			  result.put("error_msg", "标签uid不存在，非法标签");
	      }else
	      {
	    	    if (false){//(dhi.getLabelSerialNumber()!=null&&!dhi.getLabelSerialNumber().equals(serialNo)){
//	    	      result.put("return_code", "SUCCESS");
//	  			  result.put("return_msg", "标签uid与序列号不匹配");
//	  			  result.put("result_code", "FAIL");
//	  			  result.put("error_code", "GETSTATUSFAIL");
//	  			  result.put("error_msg", "标签uid与序列号不匹配");
	    	    	
	    	    }else{
	    	    	if(dhi.getAllocationStatus()!=null&&dhi.getAllocationStatus().equals("00")){
	    	    	  result.put("return_code", "SUCCESS");
	   	  			  result.put("return_msg", "标签状态为未分配");
	   	  			  result.put("result_code", "FAIL");
	   	  			  result.put("error_code", "GETSTATUSFAIL");
	   	  			  result.put("error_msg", "标签状态为未分配");
	    	    	}else{
	    	    		result.put("return_code", "SUCCESS");
	    		        result.put("return_msg", "标签状态获取成功");
	    		        result.put("result_code", "SUCCESS");
	    		        result.put("status", dhi.getReleaseStatus());
	    	    	}
	    	    }
	    	  
	    	  
	      }
		return result.toString();
	}

	@Override
	public String sourceQuery(JSONObject msg) {
		JSONObject result=new JSONObject();
		String productCode=msg.getString("productCode");
		ProductDetail pd =new ProductDetail();
		pd.setProductCode(productCode);
		List<ProductDetail> pds=productDetailMapper.selectProductDetailList(pd);
		String yugenconfig=Global.getConfig("yugenconfig");
		for(ProductDetail d :pds)
		{
			d.setImage1(yugenconfig+d.getImage1());
			d.setImage2(yugenconfig+d.getImage2());
			d.setImage3(yugenconfig+d.getImage3());
			d.setImage4(yugenconfig+d.getImage4());
			d.setImage5(yugenconfig+d.getImage5());
		}
		result.put("return_code", "SUCCESS");
		result.put("return_msg", "获取溯源信息成功");
		result.put("result_code", "SUCCESS");
		result.put("pds", pds);
		return result.toString();
	}
}
